package weeek2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

//a.有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池用一个数组int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
//创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1”，“抽奖箱2”，随机从arr数组中获取奖项元素并打印在控制台上,格式如下:
//抽奖箱1 又产生了一个 10 元大奖
//抽奖箱2 又产生了一个 100 元大奖
////.....
public
class Demo3 {
    static
    Random r=new Random();
    static ReentrantLock lock=new ReentrantLock(true);
    public static
    void main(String[] args) {
        int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
        List<Integer> list=new ArrayList<>();
        new Thread(()->{
            while (list.size()!=arr.length){
                int i= r.nextInt(arr.length);
                while (list.contains(i)){
                    i= r.nextInt(arr.length);
                }
                list.add(i);
                a(arr,i);
            }
        },"抽奖箱1").start();
        new Thread(()->{
            while (list.size()!=arr.length){
                int i= r.nextInt(arr.length);
                while (list.contains(i)){
                    i= r.nextInt(arr.length);
                }
                list.add(i);
                a(arr,i);
            }
        },"抽奖箱2").start();
    }
    public static void a(int[] a,int i){
        try {
        lock.lock();
        String name=Thread.currentThread().getName();
        System.out.println(name+" 又产生了一个 "+a[i]+" 元大奖");
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
